package com.lysj.bhp.resource.controller;



import com.lysj.bhp.resource.base.ResourceConst;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("admin/upload")
public class ResourceController {

    @PostMapping("/image")
    public Resp image(MultipartFile file, String module) {
        if (ParamUtil.isBlack(module)) {
            return new Resp(Resp.PARAM_ERROR, "请指定模块名!");
        }
        if (file == null || file.isEmpty()) {
            return new Resp(Resp.PARAM_ERROR, "文件为空!");
        }
        String filename = System.currentTimeMillis() + "";
        String path = ResourceConst.BASE_LOCAL_RESOURCE_IMAGES_DIR + module;
        String realFileName = fileUpload(file, path, filename);
        return new Resp(Resp.SUCCESS, "图片上传成功!", realFileName);
    }

    /**
     * 批量处理文件上传
     */
    @PostMapping("/filesUpload")
    public static String filesUpload(MultipartFile[] files, String path, String filename) {
        if (files == null || files.length == 0) {
            return "";
        }
        StringBuilder filenames = new StringBuilder();
        for (int i = 0; i < files.length; i++) {
            if (files[i] == null || files[i].isEmpty()) {
                break;
            }
            String fileName = files[i].getOriginalFilename();// 获取文件名
            String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 获取文件的后缀名
            String descFileName = System.currentTimeMillis() + suffixName;// 文件名
            File dest = new File(path, descFileName);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                files[i].transferTo(dest);
            } catch (IllegalStateException | IOException e) {
                e.printStackTrace();
            }
            filenames.append(descFileName).append("-");
        }
        return filenames.substring(0, filenames.length() - 1);
    }

    /**
     * 处理单文件上传 返回文件名
     */
    @PostMapping("/fileUpload")
    public static String fileUpload(MultipartFile file, String destPath, String destFileName) {


        String tempFileName = file.getOriginalFilename();// 获取文件名
        String suffixName="";
        if(tempFileName.lastIndexOf(".")>0){
            suffixName = tempFileName.substring(tempFileName.lastIndexOf(".")); // 获取文件的后缀名
        }else {
            suffixName=tempFileName;
        }


        // 判断目录是否存在,不存在则创建目录
        File thumbFile = new File(destPath + "/thumbnails/" + destFileName + suffixName); // 缩放到0.5f
        File snapshootFile = new File(destPath + "/snapshoot/" + destFileName + suffixName); // 裁切图,120x120
        String srcFileName = destFileName + suffixName; // 原图

        if(ParamUtil.isBlack(suffixName)){
            return "";
        }
        System.out.println(srcFileName);
        // 1.保存原图
        File dest = new File(destPath, srcFileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        if (!thumbFile.getParentFile().exists()) {
            thumbFile.getParentFile().mkdirs();
        }
        if (!snapshootFile.getParentFile().exists()) {
            snapshootFile.getParentFile().mkdirs();
        }


        try {
            file.transferTo(dest);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        // 2.保存缩略图
        try {
            Thumbnails.of(dest).scale(0.5).outputQuality(0.5).toFile(thumbFile);
            Thumbnails.of(dest).size(120, 120).keepAspectRatio(true).toFile(snapshootFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return srcFileName;
    }
}
